package com.neusoft.resturant.common.config;

import com.neusoft.resturant.common.interceptor.AuthInterceptor;
import com.neusoft.resturant.common.utils.Constant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @program: resturant
 * @description:
 * @author: lydms
 * @create: 2023-11-29 14:40
 **/


@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${resturant.profile}")
    private String UPLOAD_FOLDER;

    /**
     * 注入自定义拦截器
     */
    @Resource
    private AuthInterceptor authInterceptor;

    /**
     * 配置拦截器和拦截、放行路径
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // excludePathPatterns放行路径，addPathPatterns拦截的路径
        registry.addInterceptor(authInterceptor)
                .excludePathPatterns("/api/user/login", "/api/student/noAuth")
                .excludePathPatterns("/api/user/register", "/api/news/list", "/api/file/upload", "/profile/**")
                .excludePathPatterns("/restaurant/banner/**","/admin/dishes/**","/restaurant/shop/**","/api/redis/getObj/**")
                .addPathPatterns("/**");
    }

    /**
     * 求路径和服务器上的路径进行对应
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /** 本地文件上传路径 */
        registry.addResourceHandler(Constant.RESOURCE_PREFIX + "/**")
                .addResourceLocations("file:" + UPLOAD_FOLDER + "/");
    }
}
